package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.VO.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.VO.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import cn.tedu.csmall.product.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
//@RequestMapping(path={"/album","/al"})
@RequestMapping("/albums")
@Api(tags = "相册管理模块")
@Validated
public class AlbumController {
    @Autowired
    IAlbumService albumService;
    //localhost:8800/album/add-new?name=test001&description=test001&sort=100
    //localhost:8800/al/add-new?name=test001&description=test001&sort=100
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AlbumAddNewDTO albumAddNewDTO){
        albumService.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }
    //localhost:8800/al/delete
    //localhost:8800/album/delete
    //@GetMapping、@PostMapping
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除相册")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> delete(@Range(min = 1,max = 10000,message = "删除相册失败，相册ID非法！") @PathVariable Long id){
        albumService.deleteById(id);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, AlbumUpdateDTO albumUpdateDTO){
        albumService.updateInfoById(id, albumUpdateDTO);
        return JsonResult.ok();
    }
    @GetMapping("/list")
    @ApiOperation("查询相册")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<AlbumListItemVO>> select(){
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
    }
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查看相册详情")
    @ApiOperationSupport(order = 500)
    public JsonResult<AlbumStandardVO> getStandardById(@PathVariable Long id) {
        AlbumStandardVO standardById = albumService.getStandardById(id);
        return JsonResult.ok(standardById);
    }
    @GetMapping("/{name:[a-z]+}")
    @ApiOperation("根据名称查相册")
    @ApiOperationSupport(order = 600)
    public String getStandardByName(@PathVariable String name) {
        return "根据Name【" + name + "】查询相册详情";
    }
}
